const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
const tools = require("../../tools/tool");
// 通过票号查询票据明细
router.post('/bill/getDetailByNo', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let code = body.code;
    let transType = 0
    let vInvoice = null
    if(code) {
        let codearr = code.split('-');
        if (codearr.length !== 2) return ctx.err('二维码识别错误')
        // transType = tool.xe.toNumber(codearr[0]);
        let billId = tool.xe.toNumber(codearr[1]);
        vInvoice = await db.models['vInvoice'].findOne({
            where: {
                tenantId,
                id: billId,
            },
        })
    }
    else {
        let billNo = body.billNo ? body.billNo : '';
        // transType = tool.xe.toNumber(body.transType)
        if(billNo.indexOf('PZ') !== 0) {
            vInvoice = await db.models['vInvoice'].findOne({
                where: {
                    tenantId,
                    billNo
                }
            })
        }
    }
    try {
        if( body.billNo.indexOf('PZ') === 0) {
            let card = await db.models['vCard'].findOne({
                where: {
                    tenantId,
                    billNo: body.billNo
                }
            })
            if (!card) {
                return ctx.err('未找到凭证');
            }
            let list = await db.query(`select a.remark,a.subjectId,b.fullName fullName,a.price1,a.price2 from card_info 
                a inner join category b on a.subjectId=b.id and a.tenantId=b.tenantId and a.cardId=${card.id} and a.tenantId=${tenantId}`,
                {type: QueryTypes.SELECT})
            let ret = card.toJSON();
            ret.infos = list;
            return ctx.suc('', ret);
        }
        else {
            vInvoice = vInvoice.toJSON()
            let id = vInvoice.id;
            let transType = tool.xe.toNumber(vInvoice.transType)
            let billType = vInvoice.billType;
            if (['SALE', 'PUR', 'OI', 'OO'].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceInfo'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
                vInvoice.dikouList = []
                if(transType===150501 || transType === 150601) {
                    let dikou = await db.models['invoiceDikou'].findAll({
                        where: {
                            tenantId,
                            iid: id
                        }
                    })
                    vInvoice.dikouList = dikou
                }
            }
            if (['YS2YS', 'YF2YF',].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceReceipt'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
            }
            if (['YFK', 'YSK', 'SR', 'ZC', 'YS', 'YF' ].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceOther'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
            }
            if (['ZZ' ].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceTrans'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
            }
            if (['PAYMENT', 'RECEIPT' ].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceReceipt'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
                vInvoice.accounts = await db.models['invoiceOther'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
            }
            if (billType === 'YSG') {
                vInvoice.infos = await db.models['invoiceInfoYs'].findAll({
                    where: {
                        tenantId,
                        iid: id
                    }
                })
            }
            if (billType === 'PZ') {
                vInvoice.infos = await db.models['cardInfo'].findAll({
                    where: {
                        tenantId,
                        cardId: id
                    }
                })
            }
            let add = []
            if(vInvoice.linkName) add.push(vInvoice.linkName)
            if(vInvoice.linkMobile) add.push(vInvoice.linkMobile)
            if(vInvoice.address) add.push(vInvoice.address)
            vInvoice.addr = add.join(',')
            return ctx.suc('', vInvoice);
        }
    } catch (e) {
        return ctx.err(e);
    }

})
module.exports = router;